﻿using System.Management.Automation;
using System;
using Sitecore.Security.Accounts;
using System.Web;

namespace Cognifide.PowerShell.Shell.Commands
{
    [Cmdlet("Login", "User")]
    public class LoginUserCommand : BaseCommand
    {

        [Parameter(Position = 0, Mandatory = true)]
        public string User { get; set; }

        [Parameter(Position = 1, Mandatory = true)]
        public string Password { get; set; }

        [Parameter]
        public SwitchParameter Remember { get; set; }

        protected override void ProcessRecord()
        {
            RecoverHttpContext();

            Sitecore.Security.Authentication.AuthenticationManager.Login(
                    "sitecore\\"+User, Password,Remember.IsPresent);

            this.SessionState.PSVariable.Set("me", HttpContext.Current.User.Identity.Name);

/*            bool loggedIn = Sitecore.Security.Authentication.AuthenticationManager.Login(
        Login1.UserName, Login1.Password);
            if (!loggedIn)
            {
                e.Authenticated = Sitecore.Security.Authentication.AuthenticationManager.Login(
                    "sitecore\\" + Login1.UserName, Login1.Password, Login1.RememberMeSet);
            }

            /*
            if (Sitecore.Security.Accounts.User.Exists(domainUser))
            {
                Sitecore.Security.Accounts.User user =
                  Sitecore.Security.Accounts.User.FromName(domainUser, false);
                Sitecore.Security.Accounts.User.Current
                using (new Sitecore.Security.Accounts.UserSwitcher(user))
                {
                    //TODO: code to invoke as user 
                }
            } 
             */
        }

    }
}